package com.fzy.core.codetools;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.fzy.core.system.entity.Entity;

public class EntityWrapper {
	Entity data;
	ModuleWrapper parent;
	List properties;
	
	public boolean getIsTableExist(){
		return "".equals(getPrefix());
	}
	
	public List getIntf(){
		List result = new ArrayList();
//		if(getIsAuto()){
//			
//		}else{
			for (Iterator iterator = properties.iterator(); iterator.hasNext();) {
				PropertyWrapper p = (PropertyWrapper) iterator.next();
				String propertyName = p.getNameUncapital();
				String peopertyType = p.getPropertyType();
				if("id".equalsIgnoreCase(propertyName)){
					if(PropertyWrapper.STRING.equals(peopertyType))
						result.add("EntityIdString");
					if(PropertyWrapper.LONG.equals(peopertyType))
						result.add("EntityId");
				}
				if("status".equalsIgnoreCase(propertyName)){
					result.add("EntityStatus");
				}
				if("createdUser".equalsIgnoreCase(propertyName)){
					if(PropertyWrapper.STRING.equals(peopertyType))
						result.add("EntityCreatedUserString");
					if(PropertyWrapper.LONG.equals(peopertyType))
						result.add("EntityCreatedUser");
				}
				if("createdDate".equalsIgnoreCase(propertyName)){
					if(PropertyWrapper.STRING.equals(peopertyType))
						result.add("EntityCreatedDateString");
					if(PropertyWrapper.DATE.equals(peopertyType))
						result.add("EntityCreatedDate");
				}
				if("updatedUser".equalsIgnoreCase(propertyName)){
					if(PropertyWrapper.STRING.equals(peopertyType))
						result.add("EntityUpdatedUserString");
					if(PropertyWrapper.LONG.equals(peopertyType))
						result.add("EntityUpdatedUser");
				}
				if("updatedDate".equalsIgnoreCase(propertyName)){
					if(PropertyWrapper.STRING.equals(peopertyType))
						result.add("EntityUpdatedDateString");
					if(PropertyWrapper.DATE.equals(peopertyType))
						result.add("EntityUpdatedDate");
				}
			}
//		}
		return result;
	}
	
	public boolean getHasKey(){
		return getKeyProperties().size() > 0;
	}
	
	public boolean getHasId(){
		return getKeyProperties().size() == 1;
	}
	
	public boolean getHasCompositeId(){
		return getKeyProperties().size() > 1;
	}
	
	public PropertyWrapper getIdProperty(){
		return (PropertyWrapper)getKeyProperties().get(0);
	}
	
	public String getKeyColumns(){
		String result = "";
		for (Iterator iterator = getKeyProperties().iterator(); iterator.hasNext();) {
			PropertyWrapper p = (PropertyWrapper) iterator.next();
			if(result.length() < 1)
				result += p.getColumnName();
			else
				result += ", " + p.getColumnName();
		}
		return result;
	}
	
	public String getTitle(){
		String result = data.getTitle();
		if(StringUtils.isBlank(result)) result = data.getName();
		return result.trim().toLowerCase();
	}
	
	public String getComment(){
		String result = StringUtils.isBlank(data.getComment()) ? getTitle() : data.getComment();
		return result.trim();
	}

	public List getKeyProperties(){
		List result = new ArrayList();
		for (Iterator iterator = properties.iterator(); iterator.hasNext();) {
			PropertyWrapper p = (PropertyWrapper) iterator.next();
			if(p.getIsKey())
				result.add(p);
		}
		return result;
	}
	
	public String getTable() {
		String result;
		if(!StringUtils.isBlank(data.getTableName())){
			result = data.getTableName();
		}else{
			String prefixString = getPrefix();
			result = StringUtils.isBlank(prefixString) ? "" : prefixString + "_";
			result += Utils.camel2underline(data.getName());
		}
		return result.toLowerCase();
	}
	
	public String getPrefix(){
		String result = "";
		if(data.getPrefix() != null) 
			result = data.getPrefix();
		else
			result = parent.getPrefix();
		return result;
	}
	
	public String getModuleName() {
		return parent.getData().getName();
	}
		
	public String getNamespace() {
		String result = data.getNamespace();
		if(StringUtils.isBlank(result))
			result = parent.getNamespace() + "." + getModuleName();
		return result;
	}
	
	public String getPath() {
		String result = getNamespace();
		return result.replaceAll("\\.", "/");
	}
	
	public String getNameCapital() {
		return StringUtils.capitalize(data.getName());
	}
	
	public String getNameUncapital() {
		return StringUtils.uncapitalize(data.getName());
	}
//	
//	public boolean getHasNumber(){
//		boolean result = false;
//		if(!result){
//			for (Iterator iterator = properties.iterator(); iterator.hasNext();) {
//				PropertyWrapper p = (PropertyWrapper) iterator.next();
//				if(PropertyWrapper.NUMBER.equalsIgnoreCase(p.getData().getPropertyType())){
//					result = true;
//					break;
//				}
//			}
//		}
//		return result;
//	}
	
	public boolean getHasDate(){
		boolean result = false;
		if(!result){
			for (Iterator iterator = properties.iterator(); iterator.hasNext();) {
				PropertyWrapper p = (PropertyWrapper) iterator.next();
				if(PropertyWrapper.DATE.equalsIgnoreCase(p.getData().getPropertyType())){
					result = true;
					break;
				}
			}
		}
		return result;
	}
	
	/**vo显示的字段中，是否有DATE类型
	 * @return
	 */
	public boolean getHasDateOfShow(){
		boolean result = false;
		if(!result){
			for (Iterator iterator = getShowProperties().iterator(); iterator.hasNext();) {
				PropertyWrapper p = (PropertyWrapper) iterator.next();
				if(PropertyWrapper.DATE.equalsIgnoreCase(p.getData().getPropertyType())){
					result = true;
					break;
				}
			}
		}
		return result;
	}
	
	/**query查询的字段中，是否有DATE类型
	 * @return
	 */
	public boolean getHasDateOfQuery(){
		boolean result = false;
		if(!result){
			for (Iterator iterator = getQueryProperties().iterator(); iterator.hasNext();) {
				PropertyWrapper p = (PropertyWrapper) iterator.next();
				if(PropertyWrapper.DATE.equalsIgnoreCase(p.getData().getPropertyType())){
					result = true;
					break;
				}
			}
		}
		return result;
	}
	
	/**mapper显示的字段中，是否用到com.fzy.core.U类
	 * @return
	 */
	public boolean getHasUtilsOfShow(){
		boolean result = false;
		if(!result){
			for (Iterator iterator = getShowProperties().iterator(); iterator.hasNext();) {
				PropertyWrapper p = (PropertyWrapper) iterator.next();
				if((PropertyWrapper.LONG.equalsIgnoreCase(p.getData().getPropertyType())
						|| PropertyWrapper.NUMBER.equalsIgnoreCase(p.getData().getPropertyType()))){
					result = true;
					break;
				}
			}
		}
		return result;
	}
	
	public List getShowProperties(){
		List result = new ArrayList();
		for (Iterator iterator = properties.iterator(); iterator.hasNext();) {
			PropertyWrapper p = (PropertyWrapper) iterator.next();
			if(p.getIsKey()){
				result.add(p);
			}
		}
		for (Iterator iterator = properties.iterator(); iterator.hasNext();) {
			PropertyWrapper p = (PropertyWrapper) iterator.next();
			if(!p.getIsKey() && p.getIsShow()){
				result.add(p);
			}
		}
		return result;
	}
	
	public List getQueryProperties(){
		List result = new ArrayList();
		for (Iterator iterator = properties.iterator(); iterator.hasNext();) {
			PropertyWrapper p = (PropertyWrapper) iterator.next();
			if(p.getIsQuery()){
				result.add(p);
			}
		}
		return result;
	}
	
	public boolean getIsAuto(){
		return Utils.string2boolean(data.getIsAuto(), true);
	}
	
	////////////////////////////////// generated /////////////////////////////////////////////
	public Entity getData() {
		return data;
	}
	public void setData(Entity data) {
		this.data = data;
	}
	public ModuleWrapper getParent() {
		return parent;
	}
	public void setParent(ModuleWrapper parent) {
		this.parent = parent;
	}
	public List getProperties() {
		return properties;
	}
	public void setProperties(List properties) {
		this.properties = properties;
	}
	
}
